package com.koylu.validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Validator {
	
	private List rules;

	public List getRules() {
		return rules;
	}

	public void setRules(List rules) {
		this.rules = rules;
	}
	
	public void addRule(Rule rule){		
		if(rules == null){
			rules = new ArrayList();
		}
		if(findRule(rule.getName()) != null){
			throw new RuntimeException("Rule:"+rule.getName()+" Already Exist..!");
		}
		rules.add(rule);
	}
	
	public Rule findRule(String name){
		for (Iterator iterator = getRules().iterator(); iterator.hasNext();) {
			Rule rule = (Rule) iterator.next();
			if(name.equals(rule.getName())){
				return rule;
			}
		}
		return null;
	}	
	
	public String toString() {
		return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
	}
	
}
